var date = new Date();
function lpad(originalstr, length, strToPad) {
    while (originalstr.length < length)
        originalstr = strToPad + originalstr;
    return originalstr;
}
 
function rpad(originalstr, length, strToPad) {
    while (originalstr.length < length)
        originalstr = originalstr + strToPad;
    return originalstr;
}

function DateAdd(timeU,byMany,dateObj) {
	var millisecond=1;
	var second=millisecond*1000;
	var minute=second*60;
	var hour=minute*60;
	var day=hour*24;
	var year=day*365;

	var newDate;
	var dVal=dateObj.valueOf();
	switch(timeU) {
		case "ms": newDate=new Date(dVal+millisecond*byMany); break;
		case "s": newDate=new Date(dVal+second*byMany); break;
		case "mi": newDate=new Date(dVal+minute*byMany); break;
		case "h": newDate=new Date(dVal+hour*byMany); break;
		case "d": newDate=new Date(dVal+day*byMany); break;
		case "y": newDate=new Date(dVal+year*byMany); break;
	}
	return newDate;
}

function closeLightBox(linkId,boxType){
	loff();
	$('#'+linkId).removeAttr('href');
	$('#'+linkId).removeClass(boxType);
	$('#loadAjaxContent').html('');
	$('.fancybox-wrap').remove();
	$('#fancybox-overlay').remove();
}
function _renderPreauthList(){
	var fund = $('#fund').val();
	var fname = $('#txt_name').val();
	var lname = $('#txt_surname').val();
	var policy_no = $('#txt_policy_no').val();
	if(require(fund))
		alert('กรุณาเลือกบริษัทประกัน');
	else if(require(policy_no) && require(fname) && require(lname))
		alert('กรุณากรอกชื่อ, นามสกุล หรือเลขกรมธรรม์เพื่อค้นหาข้อมูล');
	else if((fname || lname) && !policy_no && (require(fname) || require(lname)))
		alert('กรุณากรอกชื่อและนามสกุล');
	else{
		lon();
		$('#loadAjaxContent').load(app_url+'/front/preauthorize/loadtpl/tpl/_popup_preauth_list',function(){
			$('#link_preauth_fund').attr('href', '#view_preauth_list');
			$('#link_preauth_fund').addClass('fancybox-ajax-light');
			$('#link_preauth_fund').click();
			$('#closeFb').click(function(){
				closeLightBox('link_preauth_fund','fancybox-ajax-light');
			});
			var fund_id = $('#fund').val();
			var searchby;
			if(policy_no)
				searchby = 'policy';
			else if(fname && lname)
				searchby = 'flname';
			else if(fname)
				searchby = 'fname';
			else if(lname)
				searchby = 'lname';
			$.ajax({
				type: "POST",
				url: (projectName+"/front/preauthorize/getpreauthbyfund/_t/"+date.getTime()).replace("//","/"),
				data: ({'fund_id':fund_id,'policy_no':policy_no,'fname':fname,'lname':lname,'searchby':searchby}),
				dataType: 'json',
				success: function(msg){
					if(msg){
						$("#tbody_view_preauth_list").html(msg.htmls);
					}
					loff();
				}
			});
		});
	}
}
function _renderPreauthConfirm(){
	lon();
	$('#loadAjaxContent').load(app_url+'/front/preauthorize/loadtpl/tpl/_popup_preauth_confirm',function(){
		$('#link_preauth_fund').attr('href', '#view_preauth_list');
		$('#link_preauth_fund').addClass('fancybox-ajax-light');
		$('#link_preauth_fund').click();
		$('#closeFb').click(function(){
			closeLightBox('link_preauth_fund','fancybox-ajax-light');
		});
		var policy_id = $('#policy_id').val();
		var policy_no = $('#policy_no').val();
		var card_no = $('#card_no').val();
		var member_name = $('#member_name').val();
		var member_lname = $('#member_lname').val();
		var nat_id = $('#nat_id').val();
		var preauth_id = $('#preauth_id').val();
		$.ajax({
			type: "POST",
			url: (projectName+"/front/preauthorize/getpreauthconfirm/_t/"+date.getTime()).replace("//","/"),
			data: ({'policy_id':policy_id,'policy_no':policy_no,'card_no':card_no,'member_name':member_name,'member_lname':member_lname,'nat_id':nat_id,'preauth_id':preauth_id}),
			dataType: 'json',
			success: function(msg){
				if(msg){
					$("#tbody_view_preauth_list").html(msg.htmls);
				}
				loff();
			}
		});
	});
}
function _renderInsertPreauth(){
	lon();
	var preauth_id = $('#preauth_id').val();
	var user_id = $('#user_id').val();
	var policy_no = $('#policy_no').val();
	var policy_id = $('#policy_id').val();
	var preauth_datetime = $('#preauth_datetime').val();
	var customer_type = $('#customer_type_desc').val();
	$.ajax({
		type: "POST",
		url: (projectName+"/front/preauthorize/insertpreauth/_t/"+date.getTime()).replace("//","/"),
		data: ({'preauth_id':preauth_id,'user_id':user_id,'policy_no':policy_no,'policy_id':policy_id,'customer_type_desc':customer_type,'preauth_datetime':preauth_datetime}),
		dataType: 'json',
		success: function(msg){
			loff();		
			//alert('บันทึกข้อมูลเรียบร้อยแล้ว');
			//window.open(app_url+'/print/'+policy_id,'_blank');
			//window.print(app_url+'/print/'+policy_id);
			closeLightBox('link_preauth_fund','fancybox-ajax-light');
			window.open(app_url+'/preauthorize','_self');
		}
	});
	window.open(app_url+'/print/'+policy_id,'_blank');	
}
function _renderPreauthListByIDCard(){
	var fund = $('#fund').val();
	var nat_id = $('#txt_idcard').val();
	if(require(fund))
		alert('กรุณาเลือกบริษัทประกัน');
	else if(require(nat_id))
		alert('กรุณากรอกหมายเลขบัตรประชาชน เพื่อค้นหาข้อมูล');
	else{
		lon();
		$('#loadAjaxContent').load(app_url+'/front/preauthorize/loadtpl/tpl/_popup_preauth_list',function(){
			$('#link_preauth_fund').attr('href', '#view_preauth_list');
			$('#link_preauth_fund').addClass('fancybox-ajax-light');
			$('#link_preauth_fund').click();
			$('#closeFb').click(function(){
				closeLightBox('link_preauth_fund','fancybox-ajax-light');
			});
			var fund_id = $('#fund').val();
			$.ajax({
				type: "POST",
				url: (projectName+"/front/preauthorize/getpreauthbyidcard/_t/"+date.getTime()).replace("//","/"),
				data: ({'fund_id' :  fund_id,'nat_id':nat_id}),
				dataType: 'json',
				success: function(msg){
					if(msg){
							$("#tbody_view_preauth_list").html(msg.htmls);
					}
					loff();
				}
			});
		});
	}
}
function _renderPaDetail(){
	lon();
	$('#loadAjaxContent').load(app_url+'/front/preauthorize/loadtpl/tpl/_popup_pa_detail',function(){
		$('#link_preauth_fund').attr('href', '#view_preauth_list');
		$('#link_preauth_fund').addClass('fancybox-ajax-light');
		$('#link_preauth_fund').click();
		$('#closeFb').click(function(){
			closeLightBox('link_preauth_fund','fancybox-ajax-light');
		});
		var policy_no = $('#policy_no').val();
		var card_no = $('#card_no').val();
		var member_name = $('#member_name').val();
		var member_lname = $('#member_lname').val();
		var nat_id = $('#nat_id').val();
		var preauth_id = $('preauth_id').val();
		$.ajax({
			type: "POST",
			url: (projectName+"/front/preauthorize/getpreauthbypadetail/_t/"+date.getTime()).replace("//","/"),
			data: ({'policy_no':policy_no,'card_no':card_no,'member_name':member_name,'member_lname':member_lname,'nat_id':nat_id,'preauth_id':preauth_id}),
			dataType: 'json',
			success: function(msg){
				if(msg){
					$("#tbody_view_preauth_list").html(msg.htmls);
				}
				loff();
			}
		});
	});
}

function _renderHealthDetail(){
	lon();
	$('#loadAjaxContent').load(app_url+'/front/preauthorize/loadtpl/tpl/_popup_health_detail',function(){
		$('#link_preauth_fund').attr('href', '#view_preauth_list');
		$('#link_preauth_fund').addClass('fancybox-ajax-light');
		$('#link_preauth_fund').click();
		$('#closeFb').click(function(){
			closeLightBox('link_preauth_fund','fancybox-ajax-light');
		});
		var policy_no = $('#policy_no').val();
		var card_no = $('#card_no').val();
		var member_name = $('#member_name').val();
		var member_lname = $('#member_lname').val();
		var nat_id = $('#nat_id').val();
		var preauth_id = $('preauth_id').val();
		var customer_type_desc = $('customer_type_desc').val();
		$.ajax({
			type: "POST",
			url: (projectName+"/front/preauthorize/getpreauthbyhealthdetail/_t/"+date.getTime()).replace("//","/"),
			data: ({'policy_no':policy_no,'card_no':card_no,'member_name':member_name,'member_lname':member_lname,'nat_id':nat_id,'preauth_id':preauth_id,'customer_type_desc':customer_type_desc}),
			dataType: 'json',
			success: function(msg){
				if(msg){
					$("#tbody_view_preauth_list").html(msg.htmls);
				}
				loff();
			}
		});
	});
}
function _renderInsertPaDetail(){
	lon();
	$('#loadAjaxContent').load(app_url+'/front/preauthorize/loadtpl/tpl/_popup_insert_pa_detail',function(){
		$('#link_preauth_fund').attr('href', '#view_preauth_list');
		$('#link_preauth_fund').addClass('fancybox-ajax-light');
		$('#link_preauth_fund').click();
		$('#closeFb').click(function(){
			closeLightBox('link_preauth_fund','fancybox-ajax-light');
		});
		var preauth_id = $('preauth_id').val();
		var user_id = $('user_id').val();
		var policy_no = $('#policy_no').val();
		var preauth_status = $('preauth_status').val();
		var customer_type_desc = $('customer_type_desc').val();
		var preauth_datetime = $('preauth_datetime').val();
		$.ajax({
			type: "POST",
			url: (projectName+"/front/preauthorize/insertpreauthbypadetail/_t/"+date.getTime()).replace("//","/"),
			data: ({'preauth_id':preauth_id,'user_id':user_id,'policy_no':policy_no,'preauth_status':preauth_status,'customer_type_desc':customer_type_desc,'preauth_datetime':preauth_datetime}),
			dataType: 'json',
			success: function(msg){
				if(msg){
					$("#tbody_view_preauth_list").html(msg.htmls);
				}
				loff();
			}
		});
	});
}
function _renderPaymentList(){
	lon();
	$('#loadAjaxContent').load(app_url+'/front/payment/loadtpl/tpl/_popup_payment_list',function(){
		$('#link_payment_list').attr('href', '#view_payment_list');
		$('#link_payment_list').addClass('fancybox-ajax-light');
		$('#link_payment_list').click();
		$('#closeFb').click(function(){
			closeLightBox('link_payment_list','fancybox-ajax-light');
		});
		var userid = $('#hid_userid').val();
		var preauth_datefrom = $('#hid_preauth_datefrom').val();
		var preauth_dateto = $('#hid_preauth_dateto').val();
		var menu_num = $('input[name=rdo_payment]:checked').val();
		$.ajax({
			type: "POST",
			url: (projectName+"/front/payment/getpaymentlist/_t/"+date.getTime()).replace("//","/"),
			data: ({'preauth_datefrom' : preauth_datefrom,'preauth_dateto' : preauth_dateto,'userid' :  userid,'menu_num' :  menu_num}),
			dataType: 'json',
			success: function(msg){
				if(msg){
						$("#tbody_view_payment_list").html(msg.htmls);
				}
				loff();
			}
		});
	});
}

function _renderPaymentConfirm(){
	lon();
	$('#loadAjaxContent').load(app_url+'/front/payment/loadtpl/tpl/_popup_payment_confirm',function(){
		$('#link_payment_list').attr('href', '#view_preauth_list');
		$('#link_payment_list').addClass('fancybox-ajax-light');
		$('#link_payment_list').click();
		$('#closeFb').click(function(){
			closeLightBox('link_payment_list','fancybox-ajax-light');
		});
		var payment_id	=	$('#hid_payment_id').val();
		var userid	=	$('#hid_user_id').val();
		var preauth_id	=	$('#hid_preauth_id').val();
		var policy_no	=	$('#hid_policy_no').val();
		var payment_datetime	=	$('#hid_payment_datetime').val();
		var payment_status	=	$('#hid_payment_status').val();
		var menu_seq = $('#hid_menu_seq').val();
		var customer_type	=	$('#hid_customer_type').val();
		var policy_id		=	$('#hid_policy_id').val();
		//var payment_type	=	$('input[name=rdo_type]:checked').val();
		//var payment_type	=	$('#rdo_type').val();
		var acc_date	=	$('#txt_acc_date').val();
		var acc_hour	=	$('#acc_hour').val();
		var acc_min	=	$('#acc_min').val();
		var admis_date	=	$('#txt_admis_date').val();
		var adm_hour	=	$('#adm_hour').val();
		var adm_min	=	$('#adm_min').val();
		var	last_acc	=	$('#accList').val();
		var deductible	=	$('#txt_deductible').val();
		var charge	=	$('#txt_charge').val();
		var discount	=	$('#txt_discount').val();
		var	co_payment	=	$('#txt_co_payment').val();
		var pre_approved	=	$('#txt_pre_approved').val();
		var non_covered	=	$('#txt_non_covered').val();
		var	indiation_admit	=	$('#tar_remark').val();
		var acc_time = acc_hour+':'+acc_min+':00.000';
		var adm_time = adm_hour+':'+adm_min+':00.000';
		var payment_type	=	$('#cover_type').val();
		$.ajax({
			type: "POST",
			url: (projectName+"/front/payment/getpaymentconfirm/_t/"+date.getTime()).replace("//","/"),
			data: ({'payment_id' : payment_id,'userid' : userid,'preauth_id' :  preauth_id,'policy_no' :  policy_no,
				'payment_datetime' : payment_datetime,'payment_status' : payment_status,'customer_type' : customer_type,'policy_id' : policy_id,
				'payment_type' : payment_type,'acc_date' : acc_date,'acc_time' : acc_time,'admis_date' : admis_date,'adm_time' : adm_time,'deductible' : deductible,
				'charge' : charge,'discount' : discount,'co_payment' : co_payment,'pre_approved' : pre_approved,
				'non_covered' : non_covered,'indiation_admit' : indiation_admit,'last_acc' : last_acc ,'menu_seq':menu_seq}),
			dataType: 'json',
			success: function(msg){
				if(msg){
						$("#tbody_view_payment_list").html(msg.htmls);
				}
				loff();
			}
		});
	});
}
function _renderInsertPayment(){
	lon();
	$('#loadAjaxContent').load(app_url+'/front/payment/loadtpl/tpl/_popup_insert_payment',function(){
		$('#link_payment_list').attr('href', '#view_preauth_list');
		$('#link_payment_list').addClass('fancybox-ajax-light');
		$('#link_payment_list').click();
		$('#closeFb').click(function(){
			closeLightBox('link_payment_list','fancybox-ajax-light');
		});
		var payment_id	=	$('#hid_payment_id').val();
		var userid	=	$('#hid_user_id').val();
		var preauth_id	=	$('#hid_preauth_id').val();
		var policy_no	=	$('#hid_policy_no').val();
		var payment_datetime	=	$('#hid_payment_datetime').val();
		var payment_status	=	$('#hid_payment_status').val();
		var menu_seq = $('#hid_menu_seq').val();
		var customer_type	=	$('#hid_customer_type').val();
		var policy_id		=	$('#hid_policy_id').val();
		var payment_type	=	$('#hid_payment_type').val();
		var acc_date	=	$('#hid_acc_date').val();
		var admis_date	=	$('#hid_admis_date').val();
		var deductible	=	$('#hid_deductible').val();
		var charge	=	$('#hid_charge').val();
		var discount	=	$('#hid_discount').val();
		var	co_payment	=	$('#hid_co_payment').val();
		var pre_approved	=	$('#hid_pre_approved').val();
		var non_covered	=	$('#hid_non_covered').val();
		var	indiation_admit	=	$('#hid_indiation_admit').val();
		var	last_acc	=	$('#hid_last_acc').val();
		$.ajax({
			type: "POST",
			url: (projectName+"/front/payment/insertpayment/_t/"+date.getTime()).replace("//","/"),
			data: ({'payment_id' : payment_id,'userid' : userid,'preauth_id' :  preauth_id,'policy_no' :  policy_no,
				'payment_datetime' : payment_datetime,'payment_status' : payment_status,'customer_type' : customer_type,'policy_id' : policy_id,
				'payment_type' : payment_type,'acc_date' : acc_date,'admis_date' : admis_date,'deductible' : deductible,
				'charge' : charge,'discount' : discount,'co_payment' : co_payment,'pre_approved' : pre_approved,
				'non_covered' : non_covered,'indiation_admit' : indiation_admit,'last_acc' : last_acc,'menu_seq':menu_seq}),
			dataType: 'json',
			success: function(msg){
				if(msg){					
					//window.open(app_url+'/printpayment/'+preauth_id,'_blank');
					$("#tbody_view_insert_payment").html(msg.htmls);
					window.open(app_url+'/payment','_self');
				}
				loff();
			}
		});
		window.open(app_url+'/printpayment/'+preauth_id,'_blank');
	});
	
}

function _renderCancelPayment(payment_id){	
	var answer = confirm('คุณต้องการยกเลิก Payment เลขที่ '+payment_id+' ใช่หรือไม่');
    if (answer){ 
    	alert('ยกเลิก Payment เลขที่:'+payment_id+' เรียบร้อย');
    	window.open(app_url+'/paymentcancel/'+payment_id,'_blank');
    	window.open(app_url+'/paymenttransection','_self');

    } 
    else{ 
    	alert('รายการ Payment ไม่ถูกยกเลิก');
    }
	
}

function _renderPaTransection(){
	var preauth_id = $('#preauth_id').val();
	var tran_start_date = $('#tran_start_date').val();
	var tran_end_date = $('#tran_end_date').val();
	if(require(preauth_id) && (require(tran_start_date) && require(tran_end_date)))
		alert('กรุณากรอก Pre-auth ID หรือ วันที่เริ่มและสิ้นสุดรายการ');
	else{
		lon();
		$('#loadAjaxContent').load(app_url+'/front/transection/loadtpl/tpl/_popup_pa_tran',function(){
			$('#link_preauth_fund').attr('href', '#view_preauth_list');
			$('#link_preauth_fund').addClass('fancybox-ajax-light');
			$('#link_preauth_fund').click();
			$('#closeFb').click(function(){
				closeLightBox('link_preauth_fund','fancybox-ajax-light');
			});
			$.ajax({
				type: "POST",
				url: (projectName+"/front/transection/getpreauthtranlist/_t/"+date.getTime()).replace("//","/"),
				data: ({'preauth_id':preauth_id,'tran_start_date':tran_start_date,'tran_end_date':tran_end_date}),
				dataType: 'json',
				success: function(msg){
					if(msg){
						$("#tbody_view_preauth_list").html(msg.htmls);
					}
					loff();
				}
			});
		});
	}
}
function _renderPaymentTransection(){
	var preauth_id = $('#preauth_id').val();
	var tran_start_date = $('#tran_start_date').val();
	var tran_end_date = $('#tran_end_date').val();
	if(require(preauth_id) && (require(tran_start_date) && require(tran_end_date)))
		alert('กรุณากรอก Pre-auth ID หรือ วันที่เริ่มและสิ้นสุดรายการ');
	else{
		lon();
		$('#loadAjaxContent').load(app_url+'/front/transection/loadtpl/tpl/_popup_payment_tran',function(){
			$('#link_preauth_fund').attr('href', '#view_preauth_list');
			$('#link_preauth_fund').addClass('fancybox-ajax-light');
			$('#link_preauth_fund').click();
			$('#closeFb').click(function(){
				closeLightBox('link_preauth_fund','fancybox-ajax-light');
			});
			$.ajax({
				type: "POST",
				url: (projectName+"/front/transection/getpaymenttranlist/_t/"+date.getTime()).replace("//","/"),
				data: ({'preauth_id':preauth_id,'tran_start_date':tran_start_date,'tran_end_date':tran_end_date}),
				dataType: 'json',
				success: function(msg){
					if(msg){
						$("#tbody_view_preauth_list").html(msg.htmls);
					}
					loff();
				}
			});
		});
	}
}
function _renderCalculate(){
	var preauth_id = $('#hid_preauth_id').val();
	var menu_seq = $('#hid_menuseq').val();
	//var cover_type = $('#cover_type').val(); //--beerz
	var cover_type;
	var AccList_type = 'x0';
	var acc_date;
	var charge = $('#txt_charge').val();
	var discount = $('#txt_discount').val();
	var Deductible;
	var er_followin = $('#hid_er_followin').val();
	var opd_waiting = $('#hid_opd_waiting').val();
	var lastAdmisOPD =$('#hid_LastAdmisOPD').val(); 
	
	admis_date =$('#txt_admis_date').val()+' '+$('#adm_hour').val()+':'+$('#adm_min').val(); //--beerz
	
	if(er_followin == undefined ) er_followin = '0D';
	if(opd_waiting == undefined ) opd_waiting = '0D';
	if(lastAdmisOPD == undefined ) lastAdmisOPD = '0';
	
	for(i=0;i<document.getElementsByName('cover_type').length;i++){ //--beerz
		if(document.getElementsByName('cover_type')[i].checked && document.getElementsByName('cover_type')[i].value != '0'){
			cover_type = document.getElementsByName('cover_type')[i].value;
			break;
		}else{
			cover_type = 'ME';
		}
	}
	
	for(i=0;i<document.getElementsByName('accList').length;i++){ //--beerz
		if(document.getElementsByName('accList')[i].checked && document.getElementsByName('accList')[i].value != '0'){
			//alert("TRUE:"+document.getElementsByName('accList')[i].value);
			acc_date =	document.getElementsByName('accList')[i].value.substr(0,document.getElementsByName('accList')[i].value.indexOf("&"));
			AccList_type = document.getElementsByName('accList')[i].value.substr(document.getElementsByName('accList')[i].value.indexOf("&")+1,2);
			break;
		}else{
			//alert("FALSE:"+document.getElementsByName('accList')[i].value);
			acc_date = $('#txt_acc_date').val()+' '+$('#acc_hour').val()+':'+$('#acc_min').val();
		}
	}
	
	for(i=0;i<document.getElementsByName('DeducList').length;i++){ //--beerz
		if(document.getElementsByName('DeducList')[i].checked && document.getElementsByName('DeducList')[i].value != '0'){
			Deductible =	document.getElementsByName('DeducList')[i].value.replace(',','');		
			break;
		}else{
			Deductible = 0;
		}
	}
	
	
	//alert(er_followin);
	//alert(opd_waiting);
	//var today = new Date();
	//alert(today);
	//alert(opd_waiting.substr(0,opd_waiting.length-1)  );

	//

	
	var d_admisDate = new Date(admis_date.substr(6,4),admis_date.substr(3,2)-1,admis_date.substr(0,2),admis_date.substr(11,2),admis_date.substr(14,2),00);
	var d_accDate = new Date(acc_date.substr(6,4),acc_date.substr(3,2)-1,acc_date.substr(0,2),acc_date.substr(11,2),acc_date.substr(14,2),00);
	
	

	var d_lastAdmisOPD = new Date(lastAdmisOPD.substr(6,4),lastAdmisOPD.substr(3,2)-1,lastAdmisOPD.substr(0,2),59,59,59);
	var d_er_followin_to = DateAdd("d",parseInt(er_followin.substr(0,(er_followin.length)-1))-1,d_accDate);
	var d_opd_waiting_to = DateAdd("d",parseInt(opd_waiting.substr(0,(opd_waiting.length)-1))-1,d_lastAdmisOPD);
	//

	if(d_admisDate > d_er_followin_to && menu_seq == 'HE' && AccList_type != 'x0'){
		alert('ระยะเวลารักษาต่อเนื่องต้องอยู่ภายใน '+er_followin.substr(0,(er_followin.length)-1)+'วัน นับจากวันเกิดเหตุ  กรุณาใช้เมนู Health-OPD');
		window.open(app_url+'/payment','_self');				
	}else if(AccList_type == 'x0'){
		er_followin = '0D';
	}
		
	
	if(parseInt(opd_waiting.substr(0,opd_waiting.length-1)) != 0 && menu_seq == 'HO' && d_admisDate <= d_opd_waiting_to ){
		alert('การรักษาครั้งนี้ อยู่ในเงื่อนไขยกเว้นของกรมธรรม์ :: OPD เว้นระยะห่าง '+opd_waiting.substr(0,opd_waiting.length-1)+' วัน นับจากวันรักษาครั้งล่าสุด (เข้ารักษา OPD ครั้งล่าสุด เมื่อวันที่ '+lastAdmisOPD+')');
	}else{
		lastAdmisOPD = '0';
	}
	
	
	if(require(acc_date))
		alert('กรุณากรอก Accident Date');
	else if(require(charge))
		alert('กรุณากรอก Charge');
	else if(require(discount))
		alert('กรุณากรอก Discount');
	else if(d_admisDate < d_accDate && menu_seq != 'HO')
		alert('Accident Date จะมากกว่า Admis Date ไม่ได้');
	else if(cover_type != AccList_type && AccList_type != 'x0'/*&& acc_date != $('#txt_acc_date').val()+' '+$('#acc_hour').val()+':'+$('#acc_min').val()*/)
		alert('TYPE ไม่ตรงกัน \r\n ================================\r\n'
				+'Type ของ  Accident List ไม่ตรงกับ  Cover Type \r\n\r\n' 
				+'Accident List ::  '+AccList_type+'\r\n'
				+'   Cover Type ::  '+cover_type
			);
	else{
		lon();
		var preapv = eval($.ajax({
			type: "POST",
			url: (projectName+"/front/payment/calpreapprove/_t/"+date.getTime()).replace("//","/"),
			data: ({'preauth_id':preauth_id,'menu_seq':menu_seq,'cover_type':cover_type,'acc_date':acc_date,'admis_date':admis_date,'charge':charge,'discount':discount, 'Deductible':Deductible, 'lastAdmisOPD':lastAdmisOPD ,'er_followin' : er_followin.substr(0,(er_followin.length)-1)}),
			async: false
		}).responseText);
		$('#txt_deductible').val(preapv[0]['deduct']);		
		$('#txt_co_payment').val(preapv[0]['copay']);
		$('#txt_pre_approved').val(preapv[0]['preapv']);
		$('#txt_non_covered').val(preapv[0]['noncover']);
		loff();
	}
	//alert(preauth_id+'@@'+menu_seq+'@@'+cover_type+'@@'+acc_date+'@@'+admis_date+'@@'+charge+'@@'+discount+'@@'+Deductible+'@@'+lastAdmisOPD+'@@'+er_followin.substr(0,(er_followin.length)-1))
}

function _renderAcclistToAccdate(){
	var acc_date;	
	for(i=0;i<document.getElementsByName('accList').length;i++){ 
		
		if(document.getElementsByName('accList')[i].checked && document.getElementsByName('accList')[i].value != '0'){
			acc_date =	document.getElementsByName('accList')[i].value.substr(0,document.getElementsByName('accList')[i].value.indexOf("&"));			
			$('#txt_acc_date').val(acc_date.substr(0,10));	
			$('#acc_hour').val(acc_date.substr(11,2));
			$('#acc_min').val(acc_date.substr(14,2));
			break;
			
		}else{
			$('#txt_acc_date').val($('#txt_admis_date').val())
			$('#acc_hour').val($('#adm_hour').val())
			$('#acc_min').val($('#adm_min').val()); 
			
		}
	}

}


